////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// jQuery
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//$(document).ready(function($) {
$(function () {
    "use strict";

//  Variables ----------------------------------------------------------------------------------------------------------

    var sceneMaxSize = $(window).width() * 3;
    var selectedTranslateX, selectedTranslateY, selectedRotation, selectedTranslateZ, selectedImage, nextImage,
        prevImage;
    var animationFinished = true;
    var $dragging = null;
    var firstClickPositionX;
    var firstClickPositionY;
    var pageWrapperLastPositionX;
    var pageWrapperLastPositionY;
    var moveX = 0;
    var moveY = 0;

    var $slide = $(".slide");
    var $body = $("body");
    var $outerWrapper = $(".outer-wrapper");
    var $videoPopup = $(".video-popup");

    var $sliderPager;
//  Pace loading screen ------------------------------------------------------------------------------------------------

    if ($(".loading-overlay").length > 0) {
        Pace.on("done", function () {
            setTimeout(function () {
                $(".loading-overlay").css("display", "none");
                // scrollByWhell(".slider-pager .owl-stage-outer");
                $sliderPager = document.querySelector(".slider-pager .owl-stage-outer");
            }, 300);
        });
    }

//  3D Parallax Slider -------------------------------------------------------------------------------------------------
    $("[data-background]").each(function () {
        if ($(this).attr("data-background").indexOf("#") != -1) {
            $(this).css("background-color", $(this).attr("data-background"));
        } else {
            $(this).css("background", "linear-gradient(to top, rgba(255, 255, 255, .65), rgba(0, 0, 0, 0)), url(" + $(this).attr("data-background") + ")");
        }
    });

    $(".inner-wrapper .slide:not(.first)").each(function (e) {
        var htmlCode;
        if (e === 0) {
            htmlCode = '<div class="item" title="单击锁定，双击打开"><a class="active" href="#' + $(this).attr("id") + '" data-order="' + (e + 1) + '">' + $(this).data("menuname") + '<span style="background-image: url(' + $(this).find(".image").attr("data-background") + ')"></span></a></div>';
        } else {
            htmlCode = '<div class="item" title="单击锁定，双击打开"><a href="#' + $(this).attr("id") + '" data-order="' + (e + 1) + '">' + $(this).data("menuname") + '<span style="background-image: url(' + $(this).find(".image").attr("data-background") + ')"></span></a></div>';
        }
        $(".slider-pager").append(htmlCode);
    });

    var slideCount = $(".slide:not(.first)").length;
    var slideRowCount = 0;
    var initX = 0, initY = 0, nextX = 0, nextY = 0;
    $(".slide:not(.first)").each(function (index, item) {
        slideRowCount += 1;
        // $(this).attr("data-position-x", randomNumber("position", sceneMaxSize));
        // $(this).attr("data-position-y", randomNumber("position", sceneMaxSize));
        $(this).attr("data-position-x", initX + nextX);
        $(this).attr("data-position-y", initY + nextY);
        if(slideRowCount == 4) {
            slideRowCount = 0;
            nextX = initX;
            nextY +=  ($(this).height() + 100);
        }else {
            nextX +=  ($(this).width() + 150);
        }
        $(this).css("left", $(this).attr("data-position-x") + "px");
        $(this).css("top", $(this).attr("data-position-y") + "px");

        // $(this).attr("data-position-z", randomNumber("position", 2000) );
        $(this).attr("data-position-z", 1);
        // $(this).attr("data-rotation", randomNumber("rotation", null) );
        $(this).attr("data-rotation", 0);
        $(this).css("transform", "rotateZ(" + $(this).attr("data-rotation") + "deg) translateZ(" + $(this).attr("data-position-z") + "px)");
    });

    var x = 0, deltax = 0;
    $(".slider-pager a").on("mousedown", function (e) {
        e.preventDefault();
        x = 0;
        deltax = e.clientX;
        $(document).bind('mouseup', end);
    });
    $(".slider-pager a").on("dblclick", function (e) {
        e.preventDefault();
        play($(this).attr("href"), true);
    });

    function end(event) {
        $(this).unbind('mousemove')
        $(this).unbind('mouseup')
        x = event.clientX - deltax
        if (x == 0) {
            play($(event.target).attr("href"), true);
        }
    }


    selectedImage = $(".slide.first")[0];
    selectedTranslateX = 0;
    selectedTranslateY = 0;

    $(".enter").on("click", function (e) {
        if(!$('input[type=checkbox][name=userAgreement]').is(':checked')) {
            $('.am-checkbox ').addClass('am-animation-shake');
            setTimeout(function () {
                $('.am-checkbox ').removeClass('am-animation-shake');
            }, 1000);
            return false;
        }
        $(".animate").removeClass("idle");
        $(".index-wrapper .description .animate").removeClass("idle"); // 毛玻璃版-系统名
        $(".index-wrapper .description").hide(); // 毛玻璃版-系统名
        e.preventDefault();
        if ($(".slide[data-menutarget!=menuBlank]").length) {
            $(selectedImage).remove();
            $(".slider-navigation").css("z-index", 999);
            nextImage = "#" + $(".slide[data-menutarget!=menuBlank]")[0].id;
            play(nextImage, true);
        } else {
            $.modal.alertWarning("系统菜单未配置，请联系管理员！");
        }
    });

    $(".next").on("click", function (e) {
        $(".animate").removeClass("idle");
        $(".index-wrapper .description .animate").removeClass("idle"); // 毛玻璃版-系统名
        $(".index-wrapper .description").hide(); // 毛玻璃版-系统名
        e.preventDefault();
        if ($(selectedImage).next().length) {
            $(".slider-navigation").css("z-index", 999);
            nextImage = "#" + $(selectedImage).next()[0].id;
            var $item_a = $(".slider-pager a[href='" + nextImage + "']");
            var left_width = ($item_a.data("order") + 1) * $item_a.width();
            if (left_width > $sliderPager.clientWidth) {
                $sliderPager.scrollLeft = (left_width - $sliderPager.clientWidth);
            }
            play(nextImage, false);
        } else {
            $sliderPager.scrollLeft = 0;
            nextImage = "#" + $slide[1].id;
            play(nextImage, false);
        }
    });

    $(".prev").on("click", function (e) {
        $(".animate").removeClass("idle");
        $(".index-wrapper .description .animate").removeClass("idle"); // 毛玻璃版-系统名
        $(".index-wrapper .description").hide(); // 毛玻璃版-系统名
        e.preventDefault();
        if ($(selectedImage).prev().length) {
            prevImage = "#" + $(selectedImage).prev()[0].id;
            var $item_a = $(".slider-pager a[href='" + prevImage + "']");
            var left_width = ($item_a.data("order") - 1) * $item_a.width();
            if (left_width == 0) {
                $sliderPager.scrollLeft = 0;
            } else if (left_width < $sliderPager.scrollLeft) {
                $sliderPager.scrollLeft = ($sliderPager.scrollLeft - left_width);
            }
            play(prevImage, false);
        } else {
            prevImage = "#" + $slide.last()[0].id;
            $sliderPager.scrollLeft = $slide.length * $(".slider-pager a[href='" + prevImage + "']").width();
            play(prevImage, false);
        }
    });

    $slide.on("dragstart", function (event) {
        event.preventDefault();
    });

    function play(_this, _open) {
        $(".index-wrapper").hide();
        $(".brand").css("z-index", 101);
        $(".slide .image i").fadeIn();
        $(".outer-wrapper").css("z-index", 0);
        $(".index-wrapper").css("z-index", -1);
        $(".index-wrapper").css("backdrop-filter", "blur(0px)");
        $(".slider-navigation").css("z-index", 999);
        $(".animate").removeClass("idle");
        $(".index-wrapper .description .animate").removeClass("idle"); // 毛玻璃版-系统名
        $(".index-wrapper .description").hide(); // 毛玻璃版-系统名

        animationFinished = false;
        $body.removeClass("zoomed-out");
        $slide.removeClass("active");
        $(".slider-pager a").removeClass("active");
        $(".slider-pager a[href='" + _this + "']").addClass("active");

        $(".slide.first .main-title").css("opacity", .5);
        $(".slide .image").each(function (e) {
            var $this = $(this);
            setTimeout(function () {
                $this.css("opacity", .5);
            }, e * 40);
        });

        selectedTranslateX = $(_this).attr("data-position-x") * -1;
        selectedTranslateY = $(_this).attr("data-position-y") * -1;
        selectedTranslateZ = $(_this).attr("data-position-z") * -1;
        selectedRotation = $(_this).attr("data-rotation") * -1;
        selectedImage = $(_this);

        $(".inner-wrapper").css({'transform': 'translateZ(-' + sceneMaxSize / 1.5 + 'px) translateX(' + selectedTranslateX + 'px) translateY(' + selectedTranslateY + 'px)'});

        selectedImage.addClass("active");
        $(".slide:not(.active)").addClass("hide-description");
        $(".slide:not(.active)").css("pointer-events", "none");
        $(".slide.active").css("pointer-events", "auto");

        if (!_open) {
            setTimeout(function () {
                $(".slide .image").css("opacity", 0);
                selectedImage.find(".image").css("opacity", 1);
                selectedImage.css("z-index", 1);
                selectedImage.find(".main-title").css("opacity", 1);
                $body.addClass("zoomed-out");
            }, 500);
        } else if (_open) {
            setTimeout(function () {
                $(".slide .image").css("opacity", 0);
                selectedImage.find(".image").css("opacity", 1);
                selectedImage.find(".main-title").css("opacity", 1);
            }, 500);
            setTimeout(function () {
                $(".inner-wrapper").css({'transform': 'translateZ(' + selectedTranslateZ + 'px) translateX(' + selectedTranslateX + 'px) translateY(' + selectedTranslateY + 'px)'});
                $outerWrapper.css({'transform': 'rotateZ(' + selectedRotation + 'deg)'});
                $(".brand").css("z-index", -1);
            }, 1500);
            setTimeout(function () {
                $(selectedImage).find(".animate").each(function (e) {
                    var $this = $(this);
                    setTimeout(function () {
                        $this.addClass("idle");
                        if ($this.parent(".description").parent('.slide').hasClass('active')) { // 毛玻璃版-系统名
                            $(".index-wrapper .description .animate").text($this.text());
                            $(".index-wrapper .fw-header .fw-header-logo .text").data('menuicon', $(_this).data("menuicon"));
                            $(".index-wrapper .fw-header .fw-header-logo .text").html('<i class="'+$(_this).data("menuicon")+'"></i>');
                            $(".index-wrapper .description .animate").addClass("idle");
                        }
                    }, e * 100);
                });
                animationFinished = true;
                $(".slide:not(.active) .image").css("opacity", "0");
                $(".slide:not(.active) .main-title").css("opacity", "0");
                $(".slide:not(.active)").addClass("hide-description");
            }, 1600);

            setTimeout(function () {
                $(".slide .image i").fadeOut();
                $(".outer-wrapper").css("z-index", -1);
                $(".index-wrapper").show();
                $(".index-wrapper").css("z-index", 2);
                $(".slider-navigation").css("z-index", 999);
                // 初始化tab按钮样式
                $(".fw-menu-add a").data("id", $(_this)[0].id);
                /*if ($(_this).data("menuicon") === "am-icon-database") {
                    $(".fw-menu-add a").removeClass().addClass("am-icon-search-plus");
                    $(".fw-menu-add a").data("icon", "am-icon-database");
                } else {
                    $(".fw-menu-add a").removeClass().addClass("am-icon-plus");
                    $(".fw-menu-add a").data("icon", "am-icon-navicon");
                }*/
                // 加载系统下级菜单
                loadSysMenus($(_this)[0].id, null);
            }, 2300);
            setTimeout(function () {
                // 初始化右键菜单
                initContextMenu();
            }, 2500);
            setTimeout(function () {
                $(".index-wrapper .description").show(); // 毛玻璃版-系统名
                // $(".index-wrapper").css("backdrop-filter", "blur(50px)"); // 毛玻璃版
                if($('.slider-navigation').hasClass("slider-navigation-open")) {
                    /*setTimeout(function () {
                        $('#fw-switch-btn').click();
                    }, 300);*/
                }
            }, 2500);
        }
    }

    if(isAgreement == '1') {
        $(".enter").click();
    }

    function randomNumber(method, sceneMaxSize, index) {
        if (method === "position") {
            return Math.floor(Math.random() * sceneMaxSize) - (sceneMaxSize / 2);
        } else if (method === "rotation") {
            return Math.floor(Math.random() * 90) + 10;
        } else {
            return false;
        }
    }

    $slide.on("click", function () {
        var _this = "#" + $(this).attr("id");
        if ($body.hasClass("zoomed-out")) {
            play(_this, true);
        }
    });

    $(".zoom-out").on("click", function (e) {
        e.preventDefault();
        $(".index-wrapper").hide();
        $(".slide:not(.active)").removeClass("hide-description");
        $(".brand").css("z-index", 101);
        $(".outer-wrapper").css("z-index", 101);
        $(".index-wrapper").css("z-index", -1);
        $(".index-wrapper").css("backdrop-filter", "blur(0px)");
        $(".slider-navigation").css("z-index", 999);
        $(".animate").removeClass("idle");
        $(".index-wrapper .description .animate").removeClass("idle"); // 毛玻璃版-系统名
        $(".index-wrapper .description").hide(); // 毛玻璃版-系统名

        var sliderLength = $(".slide").length;
        if(sliderLength < 4){
            selectedTranslateX = ($(".slide").eq(sliderLength-1).attr("data-position-x") - parseInt((150+$(".slide").width())/2)) * -1;
        }else  {
            selectedTranslateX = ($(".slide").eq(2).attr("data-position-x") -  parseInt((150+$(".slide").width())/2)) * -1;
        }
        selectedTranslateY = ($(".slide").eq(parseInt((sliderLength - 1) / 4) * 4).attr("data-position-y") - parseInt((100*(parseInt(sliderLength - 1 / 4)+1) + $(".slide").height())/2)) * -1;
        console.log(selectedTranslateX, selectedTranslateY)
        $(".inner-wrapper").css("transform", "translateZ(-4000px) translateX(" + selectedTranslateX + "px) translateY(" + selectedTranslateY + "px)");
        $outerWrapper.css("transform", "rotateZ(0deg)");
        $body.addClass("zoomed-out");
        $(".slide.first .main-title").css("opacity", .5);
        $(".slide .image").each(function (e) {
            var $this = $(this);
            setTimeout(function () {
                $this.css("opacity", .5);
            }, e * 40);
        });
        $(".slide .image i").fadeIn();

        pageWrapperLastPositionX = selectedTranslateX;
        pageWrapperLastPositionY = selectedTranslateY;

    });


    $(document.body).on("mousemove", function (e) {
        if ($body.hasClass("zoomed-out")) {
            if ($dragging) {
                console.log(selectedTranslateX, selectedTranslateY, pageWrapperLastPositionX, pageWrapperLastPositionY )
                $body.addClass("dragging");
                if(moveX == selectedTranslateX && moveY == selectedTranslateY){

                }
                moveX = pageWrapperLastPositionX + (e.pageX - firstClickPositionX) * 3;
                moveY = pageWrapperLastPositionY + (e.pageY - firstClickPositionY) * 3;
                $(".inner-wrapper").css("transform", "translateZ(-4000px) translateX(" + moveX + "px) translateY(" + moveY + "px)");

            }
        }
    });


    $(document.body).on("mousedown", ".outer-wrapper", function (e) {
        if ($body.hasClass("zoomed-out")) {
            $dragging = $(e.target);
            firstClickPositionX = e.pageX;
            firstClickPositionY = e.pageY;
            $(".inner-wrapper").css("transition", "0s");
        }
    });

    $(document.body).on("mouseup", function () {
        $body.removeClass("dragging");
        $dragging = null;
        pageWrapperLastPositionX = moveX;
        pageWrapperLastPositionY = moveY;
        $(".inner-wrapper").css("transition", "1s");
    });

    $(document.body).on("touchmove", function (e) {
        if ($body.hasClass("zoomed-out")) {
            if ($dragging) {
                $body.addClass("dragging");
                moveX = pageWrapperLastPositionX + (e.originalEvent.touches[0].pageX - firstClickPositionX)  * 3;
                moveY = pageWrapperLastPositionY + (e.originalEvent.touches[0].pageY - firstClickPositionY) * 3;
                $(".inner-wrapper").css("transform", "translateZ(-4000px) translateX(" + moveX + "px) translateY(" + moveY + "px)");
            }
        }
    });

    $(document.body).on("touchstart", ".outer-wrapper", function (e) {
        if ($body.hasClass("zoomed-out")) {
            setTimeout(function () {
                $dragging = $(e.target);
            }, 100);
            firstClickPositionX = e.originalEvent.touches[0].pageX;
            firstClickPositionY = e.originalEvent.touches[0].pageY;
            $(".inner-wrapper").css("transition", "0s");
        }
    });

    $(document.body).on("touchend", function () {
        $body.removeClass("dragging");
        $dragging = null;
        pageWrapperLastPositionX = moveX;
        pageWrapperLastPositionY = moveY;
        $(".inner-wrapper").css("transition", "1s");
    });

//  End 3D Parallax Slider ---------------------------------------------------------------------------------------------


    $(".slider-pager").owlCarousel({
        autoWidth: true,
        margin: 2
    });

});

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// On Load
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

$(window).on("load", function () {
    $(".animate").addClass("in");
});


function fontSize(){
    $(".slide:not(.first)").each(function (index, item) {
        var newFontSize = parseInt(($(this).height()/$(this).find('.description h2').text().length)*0.5);
        newFontSize = parseInt(($(this).height()/6)*0.5);
        newFontSize = newFontSize > 90 ? 90 : newFontSize;
        $(this).find('.description h2').css("font-size",  newFontSize+"px");
        $(this).find('.image i').css("font-size", (parseInt($(this).height()/1.8))+"px");
    });
}
/*fontSize();
window.onresize=fontSize;*/

if (!$("body").hasClass("iframe-page")) {
    var viewport = (function () {
        var viewPorts = ['xs', 'sm', 'md', 'lg'];

        var viewPortSize = function () {
            return window.getComputedStyle(document.body, ':before').content.replace(/"/g, '');
        };

        var is = function (size) {
            if (viewPorts.indexOf(size) === -1) throw "no valid viewport name given";
            return viewPortSize() === size;
        };

        var isEqualOrGreaterThan = function (size) {
            if (viewPorts.indexOf(size) === -1) throw "no valid viewport name given";
            return viewPorts.indexOf(viewPortSize()) >= viewPorts.indexOf(size);
        };

        // Public API
        return {
            is: is,
            isEqualOrGreaterThan: isEqualOrGreaterThan
        }

    })();
}
